<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<jsp:include page="/includes/top.jsp"></jsp:include>
<div class="page-header">
	<h1></h1>
</div>
<!-- /.page-header -->

<div class="row">
	<div class="col-xs-12">
		<div class="table-responsive">
			<table id="grid-table"
				class="table table-striped table-bordered table-hover">
				<thead>
					<tr>
						<th style="width:15px"><input type="checkbox" id="checkAll" class="ace"><span class="lbl"></span></th>
						<th>登录名</th>
						<th>姓名</th>
						<th>公司</th>
						<th>部门</th>
						<th>职务</th>
						<th>电话</th>
						<th>Email</th>
					</tr>
				</thead>
			</table>
		</div>
	</div>
</div>

<jsp:include page="/includes/bottom.jsp"></jsp:include>

<script type="text/javascript">
	var columns = [ {
						"data" : "userId",
						"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
		                    $(nTd).html("<input type='checkbox' name='checkList' value='" + sData + "' class='ace'><span class='lbl'></span>");
		                }
	                }, 
					{"data" : "userUsername"}, 
					{"data" : "userinfoTruename"}, 
					{"data" : "userinfoCompanyName"}, 
					{"data" : "userinfoDepartmentName"}, 
					{"data" : "userinfoPosition"},
					{"data" : "userinfoPhone"}, 
					{"data" : "userinfoEmail"}];
	
	$(document).ready(function() {
		//参数说明：id,url,data,column,length			
		tableload("grid-table","usergrid",null,columns,15);
		initToolbar();
		//checkbox全选
	    $("#checkAll").on("click", function () {
	        if ($(this).prop("checked")) {
	            $("input[name='checkList']").prop("checked", true);
	        } else {
	            $("input[name='checkList']").prop("checked", false);
	        }
	    });
	});

	function initToolbar(){
		$("div.toolbar").html("").append("<p>\
										<button id='btnAdd' class='btn btn-sm btn-primary add' style='display:none'><i class='icon-plus'></i>添加</button>\
										<button id='btnEdit' class='btn btn-sm btn-success edit' style='display:none'><i class='icon-pencil'></i>编辑</button>\
										<button id='btnSet' class='btn btn-sm btn-success edit' style='display:none'><i class='icon-trash'></i>分配角色</button>\
										<button id='btnDel' class='btn btn-sm btn-warning del' style='display:none'><i class='icon-trash'></i>删除</button>\
									</p>");
		$("#btnAdd").click(_addFun);
		$("#btnEdit").click(_editFun);
		$("#btnDel").click(_delFun);
		$("#btnSet").click(_setFun);
	}
	function getSelected(){
		var str = '';
	    $("input[name='checkList']:checked").each(function () {
	        str += $(this).val();
	        str += ",";
	    });
	    str = str.substr(0, str.length - 1);
	    return str;
	}
	function _addFun(){
		window.open("userform?m="+mid,"_self");
	}
	function _setFun(){
		var str = getSelected();
		if (str != '' && str.indexOf(",") < 0) {
			window.open("admin/userroleset.jsp?m=" + mid + "&id=" + str, "_self");
		} else {
			alert("请选择一条记录操作");
		}
	}
	function _editFun(){
		var str = getSelected();
		if (str != '' && str.indexOf(",") < 0) {
			window.open("userform?m=" + mid + "&users.userId=" + str, "_self");
		} else {
			alert("请选择一条记录操作");
		}
	}
	function _delFun(){
	    var str = getSelected();
		if (str != '' && str.indexOf(",") < 0) {
			if (confirm("确定要删除数据吗？")) {
				$.ajax({
				type : "post",
				url : "deluser",
				data : {'users.userId':str},				
				success : function(data) {
				    showDelMessage(data);
					window.open("admin/userlist.jsp?m="+mid,"_self");
				}
			});
			}
		} else {
			alert("请选择一条记录操作");
		}
	}
</script>